<?php
/**
 * Created by PhpStorm.
 * User: thanatos
 * Date: 2017/12/3
 * Time: 下午12:49
 */

namespace common\models;


use common\helpers\OrderHelper;

class OrderForm extends Order{
    private $_goods;
    private $_order;
    
    public function rules(){
        return [
            [['goods_id', 'user_id'], 'required'],
            [['goods_id', 'user_id'], 'integer'],
            ['goods_id', 'exist', 'targetClass' => Goods::className()],
        ];
    }
    
    /**
     * 创建订单
     * @return bool|OrderForm
     */
    public function order()
    {
        if( !$this->validate() ){
            return false;
        }
        // 查询价格
        $this->price = $this->getGoods()->goods_price * (1 - $this->getGoods()->goodsExtra->sale/100);
        $this->order_sn = OrderHelper::generateOrderSn();
        $this->pay_time = 0;
        return $this->save() ? $this : false;
    }
    
    /**
     * @return Goods
     */
    public function getGoods()
    {
        if( $this->_goods === null ){
            $this->_goods = Goods::findById($this->goods_id);
        }
        return $this->_goods;
    }
    
}